package cn.imvc.app.models;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.GoodsSku;
import cn.imvc.app.entities.Seller;
import cn.imvc.app.entities.Storage;
import cn.imvc.app.entities.User;
import cn.imvc.app.entities.UserSeller;
import cn.imvc.app.repositories.SellerRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
import cn.imvc.data.util.JpqlUtil;
import cn.imvc.data.util.PageableUtil;
/**
 * 
 * @author zhanghy
 * @date 2016年8月18日下午4:46:48
 *
 */
@Service
public class SellerModelImpl implements SellerModel{

	@Autowired
	SellerRepository  sellerRepository;
	@Autowired
	RefRelationModel refRelationModel;
	@Autowired
	ExpressModel expressModel;
	@Autowired
	StorageModel storageModel;
	@Autowired
	UserModel userModel;
	
	/**
	 * 通过当前登录用户查询商家
	 */
	public Page<Seller> find(ViewRequest viewRequest) {
		User user = userModel.getSessionUser();
		Integer userId = user.getId();
		String sql="select s.* from seller s left join user_seller b on b.sellerId = s.id where userId = " + userId;
		Page<Seller> page = sellerRepository.findAllSql(sql, PageableUtil.toPageable(viewRequest),Seller.class);
		if(page.getContent().isEmpty()){
			return sellerRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
		}else{
			return page; 	
	    }
	}

	public Seller get(int id) {
		
		return sellerRepository.getOne(id);
	}

	public int save(Seller seller) {
		seller.setCreatedAt(new Date());
		seller.setUpdatedAt(new Date());
		String code = seller.getCode();
		String name = seller.getName(); 
		List<Seller> sc = sellerRepository.getByCode(code);
		List<Seller> sn = sellerRepository.getByName(name);
		if(sc.size()>0){
			return 0;
		}else if(sn.size()>0){
			return -1;
		}
		sellerRepository.saveAndFlush(seller);
		return 1;
	}

	public int update(int id, Seller seller) {
		seller.setUpdatedAt(new Date());
		Seller loadSeller = sellerRepository.getOne(id);
		String code = seller.getCode();
		String name = seller.getName(); 
		List<Seller> sc = sellerRepository.getByCode(code);
		List<Seller> sn = sellerRepository.getByName(name);
		if(sc.size()>0&&!code.equals(loadSeller.getCode())){
			return 0;
		}else if(sn.size()>0&&!name.equals(loadSeller.getName())){
			return -1;
		}
		BeanUtils.copyProperties(seller, loadSeller, new String[]{"id","createdAt"});
		sellerRepository.saveAndFlush(loadSeller);
		return 1;
	}
	public String delete(int id) {
		String result = refRelationModel.deleteDateCheck("Seller", "id", id);
		if(result.equals("")){
			sellerRepository.delete(id);
			return "";
		}else{
			return result;
		}
		
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			
			sellerRepository.delete(id);
		}
	}

	

}
